home *** CD-ROM | disk | FTP | other *** search
/ CD ROM Paradise Collection 4 / CD ROM Paradise Collection 4 1995 Nov.iso / program / swags_z.zip / TEXTEDIT.SWG / 0008_Text Wrapping and Justify.pas < prev    next >
Pascal/Delphi Source File  |  1993-08-17  |  3KB  |  88 lines

  1. Uses CRT;
  2. var
  3.   S : string;
  4.  
  5. function Wrap(var st: string; maxlen: byte; justify: boolean): string;
  6.   { returns a string of no more than maxlen characters with the last   }
  7.   { character being the last space before maxlen. On return st now has }
  8.   { the remaining characters left after the wrapping.                  }
  9.   const
  10.     space = #32;
  11.   var
  12.     len      : byte absolute st;
  13.     x,
  14.     oldlen,
  15.     newlen   : byte;
  16.  
  17.   function JustifiedStr(s: string; max: byte): string;
  18.  
  19.     { Justifies string s left and right to length max. If there is more }
  20.     { than one trailing space, only the right most space is deleted. The}
  21.     { remaining spaces are considered "hard".  #255 is used as the char }
  22.     { used for padding purposes. This will enable easy removal in any   }
  23.     { editor routine.                                                   }
  24.  
  25.     const
  26.       softSpace = #255;
  27.     var
  28.       jstr      : string;
  29.       len       : byte absolute jstr;
  30.     begin
  31.       jstr := s;
  32.       while (jstr[1] = space) and (len > 0) do   { delete all leading spaces }
  33.         delete(jstr,1,1);
  34.       if jstr[len] = space then
  35.         dec(len);                                { Get rid of trailing space }
  36.       if not ((len = max) or (len = 0)) then begin
  37.         x := pos('.',jstr);     { Attempt to start padding at sentence break }
  38.         if (x = 0) or (x =len) then       { no period or period is at length }
  39.           x := 1;                                    { so start at beginning }
  40.         if pos(space,jstr) <> 0 then repeat        { ensure at least 1 space }
  41.           if jstr[x] = space then                      { so add a soft space }
  42.             insert(softSpace,jstr,x+1);
  43.           x := succ(x mod len);  { if eoln is reached return and do it again }
  44.         until len = max;        { until the wanted string length is achieved }
  45.       end; { if not ... }
  46.       JustifiedStr := jstr;
  47.     end; { JustifiedStr }
  48.  
  49.  
  50.   begin  { Wrap }
  51.     if len <= maxlen then begin                       { no wrapping required }
  52.       Wrap := st;
  53.       len  := 0;
  54.     end else begin
  55.       oldlen := len;                { save the length of the original string }
  56.       len    := succ(maxlen);                        { set length to maximum }
  57.       repeat                     { find last space in st before or at maxlen }
  58.         dec(len);
  59.       until (st[len] = space) or (len = 0);
  60.       if len = 0 then                   { no spaces in st, so chop at maxlen }
  61.         len := maxlen;
  62.       if justify then
  63.         Wrap := JustifiedStr(st,maxlen)
  64.       else
  65.         Wrap := st;
  66.       newlen :=  len;          { save the length of the newly wrapped string }
  67.       len := oldlen;              { and restore it to original length before }
  68.       Delete(st,1,newlen);              { getting rid of the wrapped portion }
  69.     end;
  70.   end; { Wrap }
  71.  
  72. begin
  73.   S :=
  74. 'By far the easiest way to manage a database is to create an '+
  75. 'index file. An index file can take many forms and its size will depend '+
  76. 'upon how many records you want in the db. The routines that follow '+
  77. 'assume no more than 32760 records.';
  78.  
  79. while length(S) <> 0 do
  80.   writeln(Wrap(S,75,true));
  81. Readkey;
  82. end.
  83.  
  84. Whilst this is tested and known to work on the example string, no further
  85. testing than that has been done.  I suggest you test it a great deal more
  86. before being satisfied that it is OK.
  87.  
  88.